package com.weixin.paleituo.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.activity.HouseDetailsActivity;
import com.weixin.paleituo.base.BaseRecycleAdapter;
import com.weixin.paleituo.bean.HomeHouseBean;
import com.weixin.paleituo.help.RecycleViewHolder;
import com.weixin.paleituo.util.StringUtils;
import com.weixin.paleituo.util.UIUtils;

import java.util.List;

/**
 * Author yichao
 * Time  2018/3/12 16:51
 * Dest  ${TODO}
 */

public class HomeHouseRecycleAdapter extends BaseRecycleAdapter<HomeHouseBean> {
    private List<HomeHouseBean> mDatas;

    public HomeHouseRecycleAdapter(Context context, List<HomeHouseBean> datas) {
        super(context, datas, R.layout.layout_item_home_house );
        mDatas = datas;
    }

    @Override
    protected void setData(RecycleViewHolder holder, final HomeHouseBean homeHouseBean, int position) {
        ImageView mIvIconItemFloorHome=holder.getItemView(R.id.iv_icon_item_floor_home);
        TextView mTvNameItemFloorHome=holder.getItemView(R.id.tv_name_item_floor_home);
        TextView mTvPositionItemFloorHome=   holder.getItemView(R.id.tv_position_item_floor_home);
        LinearLayout mLlTagsItemFloorHome=   holder.getItemView(R.id.ll_tags_item_floor_home);
        TextView mTvMoneyItemFloorHome=holder.getItemView(R.id.tv_money_item_floor_home);
        LinearLayout mLlRootLayoutItemFloorHome =   holder.getItemView(R.id.ll_root_layout_item_floor_home);
      View viewLine= holder.getItemView(R.id.viewLine_layout_item_floot_home);
        TextView textViewTitle=  holder.getItemView(R.id.tv_title_layout_item_home_house_fragment);
        if (position>0){
            if (homeHouseBean.getCityName().equals(mDatas.get(position-1).getCityName())){
                //相同城市
                textViewTitle.setVisibility(View.GONE);
            }  else {
                textViewTitle.setVisibility(View.VISIBLE);
                textViewTitle.setText(homeHouseBean.getCityName());
            }
        }else {
            textViewTitle.setVisibility(View.VISIBLE);
            textViewTitle.setText(homeHouseBean.getCityName());
        }
      viewLine.setVisibility(View.GONE);
        mTvNameItemFloorHome.setText(homeHouseBean.getHouses()+"");
        mTvPositionItemFloorHome.setText(homeHouseBean.getAddress()+"");
        int soldStatus = homeHouseBean.getSoldStatus();
        UIUtils.showPrice(soldStatus,mTvMoneyItemFloorHome,homeHouseBean.getMoney()+"");
       // mTvMoneyItemFloorHome.setText(homeHouseBean.getMoney()+"");
        mLlTagsItemFloorHome.removeAllViews();
        List<String> tags = homeHouseBean.getTags();
     //   LogUtils.i(TAG,tags.toString());
        int tagPosition=-1;

        if (tags!=null&& tags.size()!=0){

            for (String tag:tags){
                if (!StringUtils.isEmpty(tag)){
                    tagPosition++;
                    if (tagPosition<=2){
                        TextView textView=new TextView(mContext);
                        if (tagPosition==0){
                            textView.setTextColor(UIUtils.getColor(R.color.c_ff37f4c5));
                            textView.setBackgroundResource(R.drawable.tag_1_shape);

                        }else if (tagPosition==1){
                            textView.setTextColor(UIUtils.getColor(R.color.c_25a6e9));
                            textView.setBackgroundResource(R.drawable.tag_2_shape);
                            LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                            layoutParams.setMargins(UIUtils.dip2px(15),0,0,0);
                            textView.setLayoutParams(layoutParams);

                        }else if (tagPosition==2){
                            textView.setTextColor(UIUtils.getColor(R.color.c_d84cfb));
                            textView.setBackgroundResource(R.drawable.tag_3_shape);
                            LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                            layoutParams.setMargins(UIUtils.dip2px(15),0,0,0);
                            textView.setLayoutParams(layoutParams);
                        }


                        textView.setPadding(UIUtils.dip2px(5),UIUtils.dip2px(2),UIUtils.dip2px(5),UIUtils.dip2px(2));
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,UIUtils.px2dip(UIUtils.getDimens(R.dimen.sp_12)));
                        textView.setGravity(Gravity.CENTER);
                        textView.setText(tag);
                        mLlTagsItemFloorHome.addView(textView);
                    }


                }
            }
        }
        Glide.with(mContext).load(homeHouseBean.getImgList()).into(mIvIconItemFloorHome);
        mLlRootLayoutItemFloorHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HouseDetailsActivity.startActivity(mContext,homeHouseBean.getId()+"");
            }
        });

    }
}
